//
//  GoodShopViewController.m
//  shise
//
//  Created by 徐国庆 on 14/12/29.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "GoodShopViewController.h"
#import "NetHandler.h"
#import "Scroll.h"
#import "CrossSlipCell.h"
#import "GoodShopDetailViewController.h"

@interface GoodShopViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *array;

@end

@implementation GoodShopViewController

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [_address release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self handleData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = @"goodShop";
    CrossSlipCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        
        cell = [[[CrossSlipCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
        
    }
    Scroll *scroll = [self.array objectAtIndex:indexPath.row];
    cell.activityImageView.image = [UIImage imageNamed:@"activity.png"];
    cell.numberOfPhotosAndEndDate.text = scroll.desc;
    cell.activityNameLabel.text = scroll.title;
    [cell.crossSlipButton addTarget:self action:@selector(crossSlipButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.crossSlipButton.tag = 5000 + indexPath.row;
    for (int i = 0; i < scroll.pics.count; i ++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * 155, 5, 150, 150)];
        NSDictionary *dic = [scroll.pics objectAtIndex:i];
//        NSString *ss = [dic objectForKey:@"url"];
        [imageView setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"url"]]];
        [cell.scrollView addSubview:imageView];
        [imageView release];
        
    }
    return cell;

//    NSString *string = @"goodShop";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
//    if (cell == nil) {
//        
//        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
//    }
//    cell.textLabel.text = @"cell";
//    return cell;
}

// 设置点击横向滑动的cell的方法
- (void)crossSlipButtonAction:(UIButton *)button
{
//    NSLog(@"横向滑动");
    Scroll *scroll = [self.array objectAtIndex:button.tag - 5000];
    GoodShopDetailViewController *goodShopDetailVC = [[GoodShopDetailViewController alloc] init];
    goodShopDetailVC.address = scroll.api;
    [self.navigationController pushViewController:goodShopDetailVC animated:YES];
    [goodShopDetailVC release];
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210;
}

// 自定义处理数据的方法
- (void)handleData
{
//    NSString *string = @"https://api.shiseapp.com/v2/shop/near/?_version=2.5.0&chains=off&conf_ver=1419737046&gps=38.883260,121.540052&uuid=44cd1914a02fab9e63fb7ec1fbf910e0";
    NSString *string = [NSString stringWithFormat:@"https://api.shiseapp.com%@&conf_ver=1419737046&gps=38.883260,121.540052&uuid=44cd1914a02fab9e63fb7ec1fbf910e0", self.address];
//    NSLog(@"啦啦啦%@", string);
    [NetHandler getDataWithUrl:string completion:^(NSData *data) {
        
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *tempDic in arr) {
            
            Scroll *scroll = [[Scroll alloc] initWithDictionary:tempDic];
            [self.array addObject:scroll];
            [scroll release];
        }
        [self.tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
